var id;
var executorName;
var taskStatusDisplay;
var taskTypeDisplay ;
var nickName;
var workName ;
$(function () {
    $('#workNamess').hide()
    id = ly.getUrlParam("id");
    executorName = ly.getUrlParam("executorName");
    taskStatusDisplay = ly.getUrlParam("taskStatusDisplay");
    nickName = ly.getUrlParam("nickName");
    taskTypeDisplay = ly.getUrlParam("taskTypeDisplay");
    workName = ly.getUrlParam("workName");
    $('#taskTypeDisplay').val(taskTypeDisplay)
    $('#executorName').val(executorName)
    $('#nickName').val(nickName)
    if(workName == 'undefined'){
        $('#workNamess').hide()
    }else{
        $('#workNamess').show()
    }
    // if(workName != 'undefined'  || workName != null  || workName != undefined|| workName != ''|| workName != 'null'){
            
    //         $('#workNamess').show()
          
    // }else{

    //     $('#workNamess').hide()
    // }
    $('#workName').val(workName)
    $(".pageData").getForm({
        'ajaxform': true,
        id: id
    }, function (data) {

        var taskTypes = data.taskType;   // 2普通任务  3 巡更任务
      //  selectTaskLineByIdOnBack(data.lineId)
        var html = '';
        var list = data.taskDetailsList; 
        var list2 =data.taskReceivingRecords;
        var taskReceivingRecords = data.taskReceivingRecords;
            if(taskReceivingRecords){
            
                    if(taskReceivingRecords[0].tastStatus == 1){
                        $(".mapshow").addClass("hides")  
                        $("#lxlist").hide() 
                        ly.toast('任务未开始');
                    }else{
                            
                                var userId=taskReceivingRecords[0].userId
                                var startTime=taskReceivingRecords[0].taskStartTime
                                var endTime;
                                if(taskReceivingRecords[0].taskEndTime){
                                    endTime=taskReceivingRecords[0].taskEndTime
                                }else{
                                    endTime= times()
                                }
                                maps(userId,startTime,endTime);
                                $(".mapshow").removeClass("hides") 
                                $("#lxlist").show()  
                          }

            }

            $('#taskStatusDisplay').val(taskReceivingRecords[0].taskStatusDisplay)
        


     
           var time1,time2;
             if(taskTypes ==2){
               // $("#Map1BOX").hide()
                 if(list[0].taskCompletionRecord){
                    time1 = list[0].taskCompletionRecord.creatTime
                 }else{
                    time1 = list[0].creatTime 
                 }
             }else{
                
                $("#Map1BOX").show()
                time2=  taskReceivingRecords.creatTime
            }

    
        //     html += '</ul>'
             
        for (let i = 0; i < list.length; i++) {
            html += '<ul class="qdlist">'
            html += '<li class="qdlistli">签到点'+(i-0+1)+''
            html += '<ul class="boxlist2">'
            if(list[i].equipmentName){
                html += '<li style="border-bottom: 1px solid #ccc;">'+list[i].equipmentName+'</li>'
            }else{
                html += '<li style="padding-top: 14px;" ></li>'
            }
           
             if(taskTypes ==2){
                if(list[i].taskCompletionRecord  ){
                    html += '<li>签到时间：'+time1+'</li>'
                     }else if(taskReceivingRecords[0].taskStatusDisplay=="进行中"){
                        html += '<li>开始时间：'+data.taskReceivingRecords[0].taskStartTime+'</li>'     
                     } else{
                        html += '<li>开始时间：'+time1+'</li>'     
                     }
             }else if(taskTypes ==3){
                if(data.taskReceivingRecords[0].taskStatusDisplay == '已完成'){
                    html += '<li>签到时间：'+list[i].taskCompletionRecord.creatTime+'</li>'   
                }else if(data.taskReceivingRecords[0].taskStatusDisplay == '已撤销'){
                   html += '<li>撤销时间：'+data.taskReceivingRecords[0].taskEndTime+'</li>'   
                }else if(data.taskReceivingRecords[0].taskStatusDisplay == '任务暂停'){
                    html += '<li></li>'   
                 }
                // html += '<li>签到时间'+time2+'</li>'
             }
       
        //   else if(list[i].taskReceivingRecords){
        //     html += '<li>'+list[i].taskReceivingRecords.taskStatusDisplay+'</li>'
        //   } 
          else{
            html += '<li>未签到</li>'
           }
            html += ' </ul>'
            html += ' </li>'
            html += ' </ul>'
        }
        $('.qdlist').html(html);
        var points = [
            {
                data:1,
                lng:'116.3786889372559',
                lat:'39.39534009082035', 
            } ,
            {
                data:1,
                lng:'116.3786889372559',
                lat:'39.90789300648029', 
            }    

       ];
      
  
    });

   



});




function times (){
    var date = new Date();

var year = date.getFullYear();

var month = date.getMonth()+1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();

return year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second;
}

var map = new AMap.Map("container", {
    center: [106.58463134765626,29.533822970920138],
    zoom: 14
});


function maps(userId,startTime,endTime){
    var points = [];
    var datas = {
        'userId':userId,
        'startTime':startTime,
        'endTime':endTime,
    };
    var url = site.apiUrl + "/all/userMotionTrack/selectMotionTrackByUserIdAndTime"; //apiUrl

    request({
        url: url,
        data: datas,
        type: "get",
        ajaxform: true,
        callback: function (f) {
            var datas = f.result;
            //console.log(datas)
            if (f.code == "rest.success") {
            
              
                if(datas == ''  || datas.length <2){
                    ly.toast('暂无最近轨迹记录');    
                }else{
                    $.each(datas, function (i, o) {
                        points.push({
                            lng:o.uLog,
                            lat:o.uLat,
                        })
                    });
                    maps3(points)
                }
            
            
               // maps2(points)
            } else {
                
            }
        }
    });


}





//  function maps3(points){

//     var endArr = points[0];
//     var starArr= points[points.length-1];
//     var tjArr = [];

//     for (let i = 1; i < points.length; i++) {
//         tjArr.push(points[i]) 
//     }
//     tjArr.pop()

//     var tjArr3=[];
//     for (let i = 0; i < tjArr.length; i++) {
//          tjArr3.push(  new AMap.LngLat(tjArr[i].lng, tjArr[i].lat))
//     }

//     driving.search(
//         new AMap.LngLat(starArr.lng, starArr.lat), 
//         new AMap.LngLat(endArr.lng, endArr.lat),
//         {
//             waypoints:tjArr3
//             // waypoints:[
//             //     new AMap.LngLat(116.379028, 39.885042)
//             // ]
//         },function(status, result) {
//         // result即是对应的驾车导航信息，相关数据结构文档请参考 https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult
//         if (status === 'complete') {
//             if (result.routes && result.routes.length) {
//                 // 绘制第一条路线，也可以按需求绘制其它几条路线
//                 drawRoute(result.routes[0])
               
//             }
//         } else {
           
//         }
//     });
//  }
  

function maps3(points){
    console.log(points);
    var endArr = new AMap.LngLat(points[0].lat, points[0].lng)
    var starArr= points[points.length-1];
    var starArr2= new AMap.LngLat(starArr.lat, starArr.lng)
   
    var tjArr3=[];

    for (let i = 0; i < points.length; i++) {
 
        tjArr3.push( [points[i].lng,points[i].lat])
    } 

    console.log(starArr.lat+','+starArr.lng)
    var startMarker = new AMap.Marker({
        position: new AMap.LngLat(starArr.lng, starArr.lat),
        icon: 'https://webapi.amap.com/theme/v1.3/markers/n/start.png',
        map: map
    })
    var endMarker = new AMap.Marker({
        position: new AMap.LngLat(points[0].lng, points[0].lat),
        icon: 'https://webapi.amap.com/theme/v1.3/markers/n/end.png',
        map: map
    })

    var routeLine = new AMap.Polyline({
        path: tjArr3,
        isOutline: true,
        outlineColor: '#3366FF',
        borderWeight: 1,
        strokeColor: "#3366FF", 
        strokeOpacity: 1,
        strokeWeight: 2,
        // 折线样式还支持 'dashed'
        strokeStyle: "solid",
        // strokeStyle是dashed时有效
        strokeDasharray: [10, 5],
        lineJoin: 'round',
        lineCap: 'round',
        zIndex: 50,
    })

   

  

    routeLine.setMap(map)  


    //map.add([polyline,startMarker, endMarker]);
    map.setFitView([startMarker,endMarker,routeLine ])







}





function selectTaskLineByIdOnBack(lineId){

    var url = site.apiUrl + "/back/taskLine/selectTaskLineByIdOnBack"; //apiUrl
    var data = {
        'id': lineId,
    };
    
    request({
        url: url,
        data: data,
        type: "get",
        ajaxform: true,
        callback: function (f) {
            console.log(f);
            if (f.code == "rest.success") {
                var lists = f.result.taskDetailsList;
              
                    for (let i = 0; i < lists.length; i++) {
                  
                        return lists[i].creatTime
                        
                    }
                  

            } else {
                ly.toast(f.desc);
            }
        }
    });
}
/*
 * 提交表单
 */
function submitFom() {


    var taskName = $("#taskName").val();
    var taskStatement = $("#taskStatement").val();


    var taskExceptionDescription = {
        'exeptionContext': taskStatement,
        'exceptionStatus': eventStar,
        'exceptionId': id,
        'taskId': task_id,
        'userId': userId
    };

    var url = site.apiUrl + "/back/task/updateExceptionTask"; //apiUrl


    request({
        url: url,
        data: taskExceptionDescription,
        type: "get",
        ajaxform: true,
        callback: function (f) {
            console.log(f);
            if (f.code == "rest.success") {

                ly.alert('修改成功', function () {
                    ly.goto("task-fail.html");
                });

            } else {
                ly.toast(f.desc);
            }
        }
    });

}